package com.arnold.www.atomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.*;

/**
 * @ClassName AtomicTest
 * @Description: 原子类的常用类节本介绍使用
 * @Author Arnold
 * @Date 2020/4/12
 * @Version V2.0
 **/
public class AtomicTest {
    //整型数据
    //private int value = 0;
    private AtomicInteger value = new AtomicInteger(0);
    //整形数组数据
    private AtomicIntegerArray integerArray = new AtomicIntegerArray(new int[]{1,2,3});
    //抽象类型数据
    private AtomicReference<User> atomicReference = new AtomicReference();
    //抽象类对象的字段
    private AtomicIntegerFieldUpdater<User> atomicReferenceUpdate =  AtomicIntegerFieldUpdater.newUpdater(User.class,"age");


    public int getValue(){
        //return value++;
        return value.getAndIncrement();
    }

    public int getArrayValue(){
        //return value++;
        return integerArray.addAndGet(0,4);
    }
    public User getReferenceValue(){
        //return value++;
        atomicReference.set(new User("小明",18,"男"));
        return atomicReference.get();
    }
    public int updateReferenceValue(){
        //return value++;
        User user = new User("小红",18,"女");
        atomicReferenceUpdate.set(user,20);
        return atomicReferenceUpdate.get(user);
    }

    public static void main(String[] args) {

        final AtomicTest atomicTest =new AtomicTest();

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        /*executorService.submit(new Runnable() {
            public void run() {
                for (int i=0;i<10;i++) {
                    System.out.println(Thread.currentThread().getName()+"\t"+atomicTest.getValue());
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        executorService.submit(new Runnable() {
            public void run() {
                for (int i=0;i<10;i++) {
                    System.out.println(Thread.currentThread().getName() +"\t"+atomicTest.getValue());
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });*/
        /*executorService.submit(new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() +"\t"+atomicTest.getArrayValue());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });*/

        /*executorService.submit(new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() +"\t"+atomicTest.getReferenceValue().toString());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });*/
        executorService.submit(new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() +"\t"+atomicTest.updateReferenceValue());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.shutdown();
    }
}